package py.com.erpone.session.dm;

import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import py.com.erpone.entity.dm.Departamento;
import py.com.erpone.entity.dm.Empresa;



@Stateful
@Named
@LocalBean
@ConversationScoped
public class DepartamentoBean {

	@PersistenceContext(type=PersistenceContextType.EXTENDED)
	private EntityManager em;
	
	@Inject
	private Conversation conversation;
	
	private Departamento departamento;	

	public Departamento getDepartamento() {
		return departamento;
	}

	public void setDepartamento(Departamento departamento) {
		this.departamento = departamento;
	}

	public String iniciar(){
		if(conversation.isTransient())
			conversation.begin();
		departamento=new Departamento();
		departamento.setEmpresa(new Empresa());
		return "/dm/departamento.xhtml";
	}
	
	public String iniciar(Departamento departamento){
		if(conversation.isTransient())
			conversation.begin();
		this.departamento=departamento;
		return "/dm/departamento.xhtml";
	}
	
	
	public String guardar(){
		if(departamento.getId()==null){
			em.persist(departamento);
		}else
			em.merge(departamento);
		if(!conversation.isTransient())
			conversation.end();
		return "/dm/departamentos.xhtml";
	}
	
	public String eliminar(Departamento departamento){
		em.remove(em.merge(departamento));
		if(!conversation.isTransient())
			conversation.end();
		return "/dm/departamentos.xhtml";
	}
	
	public String cancelar(){
		if(!conversation.isTransient())
			conversation.end();
		return "/dm/departamentos.xhtml";
	}
	
}
